import {Component, OnInit, OnDestroy, ViewChild, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute, Router, Params} from "@angular/router";
import {Content} from "@shared/components/toolbar/toolbar-content";
import {Subject} from "rxjs";
import {ToolsService} from "@shared/service/tools/tools.service";
import {ToolbarService} from "@shared/components/toolbar/toolbar.service";
import {SeoService} from "@core/seo/seo.service";
import {NewsService} from "@news/news.service";
import {ArticleCommentVO, ArticleVO} from "@interface/index";



@Component({
  selector: 'seed-news',
  templateUrl: './news.component.html',
  styleUrls: ['./news.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class NewsComponent implements OnDestroy, OnInit {
  articleId: ArticleVO;
  article: ArticleVO = new ArticleVO();
  comments: ArticleCommentVO[] = [];
  recommend: ArticleVO[] = [];
  hasMoreData = false;
  @ViewChild(Content) content;
  public ngUnsubscribe: Subject<void> = new Subject<void>();

  constructor(
    public router: Router,
    public tools: ToolsService,
    public route: ActivatedRoute,
    public newService: NewsService,
    private _seoService: SeoService,
    public toolBarService: ToolbarService
  ) {

    this.route.params
      .takeUntil(this.ngUnsubscribe)
      .switchMap((params: Params) => {
        this.tools.showLoading();
        return this.newService.GetCateArticleInfo(params['articleId']);
      })
      .subscribe((res) => {
        this.tools.hideLoading();
        if (res && res.result === '0') {
          this.article = res.data;
          this._seoService.setTKD(
            `${this.article.title}_体育资讯`,
            `${this.article.title}_${this.article.cateName}`,
            `求苗体育聚集各类体育产业IP，包含但不限于体育赛事、体育培训、体育旅游、体育赞助、体育用品等各大板块，
      实现多种资源的聚合与互通, 赛事运营管理、合作机构对接、消费数据统计及线上推广等多服务轻松一键搞定的健康运动服务的互联网平台`
          );
          this.content.scrollTop();
          this.newService.GetCateArticleListComment(this.article.articleId)
            .subscribe(dates => {
              if (dates && dates.result === '0' && dates.data.list) {
                this.comments = [...dates.data.list];
                if (dates.data.hasNextPage) {
                  this.hasMoreData = true;
                }
              }
            });

          this.newService.GetCateArticleListRecommend(res.data.cateId, res.data.articleId)
            .takeUntil(this.ngUnsubscribe)
            .subscribe((dates) => {
              if (dates && dates.result === '0') {
                this.recommend = [...dates.data];
              }
            });
        }
      });
  }


  ngOnInit() {

  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

}
